package cn.edu.zut.cs.DAO;

import cn.edu.zut.cs.module.Book;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class BookDAOImpl extends AbstrckBookDAO {
    @Override
    public void insertBook(Book book) {
        super.insertBook(book);
        String INSERT_BOOK="INSERT INTO book(isdn,name,price) VALUES(?,?,?)";
        Object[] args= {book.getIsdn(),book.getName(),book.getPrice()};
        jdbcTemplate.update(INSERT_BOOK,args);
    }

    @Override
    public void modifyBook(Book book) {
        super.modifyBook(book);
        String MODIFY_BOOK="update book set name=?,price=? where isdn=?";
        Object[] args= {book.getIsdn(),book.getName(),book.getPrice()};
        jdbcTemplate.update(MODIFY_BOOK,args);
    }

    @Override
    public List<Book> queryAll() {
        String QUERY_ALL_BOOK = "select * from book";
        RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
        books=jdbcTemplate.query(QUERY_ALL_BOOK,rowMapper);
        return books;
    }
}
